<template>
	<div class="login">
		<el-form ref="form" :model="form" label-width="80px">
			<el-form-item label="账号">
				<el-input v-model="form.phone"></el-input>
			</el-form-item>
			<el-form-item label="密码">
				<el-input type="password" v-model="form.password"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="onSubmit">登录</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					phone: '',
					password: '',
				}
			}
		},
		methods: {
			onSubmit() {
				// 发送 POST 请求
				this.$axios({
					method: 'post',
					url: this.$api+'/app3/consumer/loginConsumer',
					data: {
						phone: this.form.phone,
						password: this.form.password,
						type:'1'
					}
				}).then(res=>{
					if(res.data.code==200){
						console.log(res.data,'登录')
						localStorage.setItem('token', res.data.data.token);
						this.getuserinfo()
						// localStorage.setItem('name', res.data.data.name);
						this.$router.push('/optimization')
						
					}else{
						 this.$message({
						  type: 'info',
						  message: res.data.msg
						});
					}
					
					console.log(res)
				})
				// console.log('submit!');
			},
			getuserinfo(){
				this.$axios({
					method: 'post',
					url: this.$api+'/app3/consumer/queryConsumerInfo',
					data: {},
					headers:{
						token:localStorage.getItem('token')
					}
				}).then(res=>{
					if(res.data.code==200){
						console.log(res.data)
						localStorage.setItem('address', res.data.data.address);
						localStorage.setItem('name', res.data.data.name);
						localStorage.setItem('phone', res.data.data.phone);
					}else{
						 this.$message({
						  type: 'info',
						  message: res.data.msg
						});
					}
					
					console.log(res)
				})
			}
		}
	}
</script>

<style>
	pages {
		background-color: #000;
	}

	.login {
		width: 50%;
		height: 60%;
		position: absolute;
		top: 10%;
		left: 20%;
		/* border: 1px solid red; */
	}
</style>
